/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bygf.dts;

import bygf.dts.db.MariaDB;
import bygf.dts.gui.MainJFrame;
import bygf.dts.gui.TestJFrame;
import bygf.dts.utility.MessageShow;
import bygf.dts.utility.GetFilePath;
import com.google.gson.Gson;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONObject;


/**
 *
 * @author pi
 */
public class Dts {

    private static Logger logger = LogManager.getLogger(Dts.class.getName());
    private static boolean isRunning() {
        boolean rv = false;
        try {
            String os_name = System.getProperty("os.name");
            // 指定文件锁路径
            String path = null;
            if (os_name.contains("Windows")) {
                // 假如是Windows操作系统
                path = System.getProperty("user.home")
                        + System.getProperty("file.separator");
            } else {
                path = "/home/pi/lock/";
            }
            File dir = new File(path);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            // 程序名称
            String applicationName = "JavaDts";
            RandomAccessFile fis = new RandomAccessFile(path + applicationName
                    + ".lock", "rw");
            FileChannel lockfc = fis.getChannel();
            FileLock flock = lockfc.tryLock();
            if (flock == null) {
                rv = true;
            }
        } catch (FileNotFoundException e1) {
            logger.error("异常",e1);
        } catch (IOException e) {
            logger.error("异常",e);
        }
        return rv;
    }
    private static void setLookAndFeel(){
         try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            logger.error("异常",ex);
        }   
    }
    public static void main(String[] args) { 
        setLookAndFeel();
        if (isRunning()) {
            MessageShow.errorDialog("软件正在运行");
            System.exit(0);
        }
        logger.debug("软件运行");
        java.awt.EventQueue.invokeLater(() -> {
            MainJFrame mainFrame = new MainJFrame();
            mainFrame.setVisible(true);
        });
    }

}
